package com.shiguiwu.springmybatis.spring.lifecycle.instance;

import cn.hutool.core.collection.CollUtil;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor;

import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 自定义候选构造器
 * @author: stone
 * @date: Created by 2021/3/24 23:42
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.spring.lifecycle.instance
 */
public class MySmartInstantiationAwareBeanPostProcessor implements SmartInstantiationAwareBeanPostProcessor {
    @Override
    public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException {
        System.out.println("bean的名称：" + beanName);
        System.out.println(beanClass.getName());
        System.out.println("调用 SmartInstantiationAwareBeanPostProcessor#determineCandidateConstructors ");

        Constructor<?>[] constructors = beanClass.getDeclaredConstructors();
        Constructor[] array = Arrays.stream(constructors)
                .filter(c -> c.isAnnotationPresent(MyAutowire.class)).toArray(Constructor[]::new);
        return (array.length!=0) ? array : null;
    }
}
